<template>
  <header class='app-header'>
    <div class="container">
      <h1 class="logo">
        <RouterLink to="/">小兔鲜</RouterLink>
      </h1>

      <!-- 导航组件 -->
      <app-header-nav />

      <div class="search">
        <i class="iconfont icon-search"></i>
        <input v-model="keyword" @keyup.enter="search" type="text" placeholder="搜一搜" />
      </div>

      <!-- 购物车头部组件 -->
      <app-header-cart />
    </div>
  </header>
</template>

<script>
import AppHeaderNav from './app-header-nav.vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import AppHeaderCart from './app-header-cart.vue'

export default {
  name: 'AppHeader',
  components: { AppHeaderNav, AppHeaderCart },
  // - 双向绑定输入框
  // - 绑定按键且enter事件
  // - 跳转搜索地址，携带搜索关键字
  // - 清空输入框，失去焦点
  setup() {
    const keyword = ref('')
    // 等价与 选项API中的 this.$router
    const router = useRouter()
    const search = e => {
      // 实际开发：判断是否输入关键字，但是现在数据少，允许不输入内容可以跳转
      // 进行跳转，使用编程式导航
      router.push(`/search?keyword=${keyword.value}`)
      // 手动置空输入框
      keyword.value = ''
      // 通过dom的blur函数失去焦点
      e.target.blur()
    }
    return { keyword, search }
  }
}
</script>

<style scoped lang='less'>
.app-header {
  background: #fff;
  .container {
    display: flex;
    align-items: center;
  }
  .logo {
    width: 200px;
    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url(../assets/logo.png) no-repeat center 18px / contain;
    }
  }
  .search {
    width: 170px;
    height: 32px;
    position: relative;
    border-bottom: 1px solid #e7e7e7;
    line-height: 32px;
    .icon-search {
      font-size: 18px;
      margin-left: 5px;
    }
    input {
      width: 140px;
      padding-left: 5px;
      color: #666;
    }
  }
  .cart {
    width: 50px;
    .curr {
      height: 32px;
      line-height: 32px;
      text-align: center;
      position: relative;
      display: block;
      .icon-cart {
        font-size: 22px;
      }
      em {
        font-style: normal;
        position: absolute;
        right: 0;
        top: 0;
        padding: 1px 6px;
        line-height: 1;
        background: var(--help-color);
        color: #fff;
        font-size: 12px;
        border-radius: 10px;
        font-family: Arial;
      }
    }
  }
}
</style>